<template>
  <div class="home-page">
    <h1>
      疯帽子家社团联盟
      <span class="hat-icon" @click="togglePopup">🎩</span>
    </h1>
    <link href="https://fonts.googleapis.com/css2?family=ZCOOL+KuaiLe&display=swap" rel="stylesheet">
    <el-backtop :right="40" :bottom="50" />


    <div class="search-container">
      <div class="search-bar">
        <input v-model="searchQuery" type="text" placeholder="搜索你感兴趣的社团" @keyup.enter="goToSearchResult" />
        <button class="search-button" @click="goToSearchResult">
          <i class="el-icon-search"></i> 搜索
        </button>

        <el-icon>
          <Search />
        </el-icon>
      </div>
    </div>

    <!-- 更多社团链接 -->
    <div class="more-clubs">
      <el-button class="explore-button" type="primary" @click="goToClubList" icon="el-icon-plus">
        探索更多社团...
      </el-button>
    </div>

    <!-- 排行榜 -->
    <div class="rankings-wrapper">
      <el-row :gutter="20" class="rankings-row">

        <!-- 不同的榜单 -->
        <el-col :xs="24" :sm="8" v-for="(ranking, index) in rankings" :key="index">
          <el-card class="ranking-category" shadow="hover">
            <template #header>
              <div class="ranking-title">
                <span>{{ ranking.title }}</span>
                <el-icon>
                  <component :is="ranking.icon" />
                </el-icon>
              </div>
            </template>

            <!-- 榜单内容 -->
            <ul class="ranking-list" v-loading="rankings[index].loading">
              <li class="ranking-item" v-for="category in ranking.categorys" :key="category.id"
                @click="ranking.action(category)">
                <span class="ranking-position">{{ category.position }}</span>
                <el-icon>
                  <Star />
                </el-icon>
                <span class="ranking-name text-truncate">{{ category.name }}</span>
              </li>
            </ul>
            <!-- 添加图片到社团分类卡片底部 -->
            <div v-if="ranking.title === '社团分类'" class="image-container">
              <img :src="imageURL" alt="Description of the image" class="bottom-image" @click="picturePopup">
            </div>
          </el-card>
        </el-col>

      </el-row>
    </div>
  </div>
</template>

<script>
import { Trophy, Star, Menu } from '@element-plus/icons-vue'
import axios from "axios";
import { ElMessage, ElNotification } from 'element-plus';

export default {
  name: "HomePage",
  components: {
    Trophy,
    Star,
    Menu
  },
  data() {
    return {
      searchQuery: '',
      imageURL: 'https://pic.imgdb.cn/item/6698c9afd9c307b7e974f2cd.png', // 设置为你的图片URL
      rankings: [
        {
          title: '热门推荐',
          icon: 'Trophy',
          action: this.goToClubDetail,
          categorys: [],
          loading: true
        },
        {
          title: '最新发布',
          icon: 'Star',
          action: this.goToClubDetail,
          categorys: [],
          loading: true
        },
        {
          title: '社团分类',
          icon: 'Menu',
          action: this.goToClubList,
          categorys: [],
          loading: true
        }
      ],

      isShowEaster:false,
      bgStyle:""
    };
  },

  created() {
    this.getPopularCulbList();
    this.getLatestClubList();
    this.getClubCategoryList();
  },

  methods: {
    generateRandomGradient() {
      //生成随机渐变背景
      const randomRGB = () => {
        const red = Math.floor(Math.random() * 256);
        const green = Math.floor(Math.random() * 256);
        const blue = Math.floor(Math.random() * 256);

        // 固定透明度为0.7
        return `rgba(${red}, ${green}, ${blue}, 0.5)`;
      };

      const randomAngle = Math.floor(Math.random() * 360);
      const color1 = randomRGB();
      const color2 = randomRGB();
      return `background-image: linear-gradient(${randomAngle}deg, ${color1}, ${color2})`;
    },
    togglePopup() {

      if(!this.isShowEaster){
        this.isShowEaster=true;
        this.bgStyle=this.generateRandomGradient();
        ElNotification({
          title: '🎩',
          message: `
          <p>
          亲爱的用户:<br>
          我们是<strong>疯帽子家开发团队</strong>。<br>
          感谢您选择加入我们的
          “<strong style="${this.bgStyle}">疯帽子家社团联盟</strong>”
          ——
          一个既合理又充满欢乐与幽默的社团管理与交流平台。<br><br>
          在这里，我们不仅仅是在管理社团，更是在享受每一次的创意碰撞和团队协作。<br>
          在这段代码的深处，隐藏着我们开发团队的小小秘密——我们对您的深深感谢。<br>
          每当您点击一个按钮，提交一个表单，或是在平台上留下您的足迹，我们都能感受到您对我们项目的信任和支持。
          </p>`,
          duration: 5000,
          dangerouslyUseHTMLString: true,
          onClose:()=>{
            ElMessage.success("感谢您的支持！")
            this.isShowEaster=false;
          }
        });
      }


    },
    picturePopup(){
      ElMessage.success("更多功能尽情期待~")
    },

    //获取热门社团信息
    getPopularCulbList() {
      axios.get("/index/popular")
        .then(response => {

          // 处理服务器返回的数据
          const records = response.data.data;
          this.rankings[0].categorys = records?.map((record) => ({
            id: record.clubId,
            name: record.clubName
          }))

          this.rankings[0].loading = false;
        })
    },

    //获取最新社团信息
    getLatestClubList() {
      axios.get("/index/latestClubs")
        .then(response => {
          const records = response.data.data;
          this.rankings[1].categorys = records?.map((record) => ({
            id: record.clubId,
            name: record.clubName
          }))

          this.rankings[1].loading = false;
        })
    },

    //获取社团分类列表
    getClubCategoryList() {
      axios.get("/Attribute/ClubCategoryList")
        .then(response => {
          this.rankings[2].categorys = response.data.data?.map((item, index) => ({
            id: index,
            name: item
          }));

          this.rankings[2].loading = false;
        })
    },

    goToSearchResult() {
      if (this.searchQuery.trim()) {
        // 使用路由查询参数传递搜索关键词
        this.$router.push({ path: '/search-result', query: { keyword: this.searchQuery } });
      }
    },

    goToClubDetail(club) {
      this.$router.push({ name: "ClubDetail", query: { clubId: club.id } });
    },

    goToClubList(category) {
      this.$router.push({
        name: "ClubList",
        query: { selectedCategory: category.name }
      });
    }
  }
};
</script>

<style scoped>

/* 渐变背景动画 */
@keyframes gradientBG {
  0% {
    background-position: 0% 50%;
  }

  50% {
    background-position: 100% 50%;
  }

  100% {
    background-position: 0% 50%;
  }
}

/* 标题弹出动画 */
@keyframes titlePop {
  0% {
    transform: scale(0.8);
    opacity: 0;
  }

  100% {
    transform: scale(1);
    opacity: 1;
  }
}

/* 脉冲动画 */
@keyframes pulse {
  0% {
    transform: scale(1);
  }

  50% {
    transform: scale(1.05);
  }

  100% {
    transform: scale(1);
  }
}

/* 旋转动画 */
@keyframes rotate {
  from {
    transform: rotate(0deg);
  }

  to {
    transform: rotate(360deg);
  }
}

/* 跳跃动画 */
@keyframes bounce {

  0%,
  100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-10px);
  }
}


/* 标题旋转动画 */
@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}


/* 页面整体样式 */
.home-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: linear-gradient(135deg, #ffe0b2, #fff9c4);
  background-size: 400% 400%;
  padding: 60px 20px;
  min-height: 100vh;
  animation: gradientBG 10s ease infinite;
  position: relative;
  overflow: hidden;
}

/* 纹理图案 */
.home-page::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('https://www.transparenttextures.com/patterns/45-degree-fabric-dark.png') repeat;
  /* 更为明显的纹理图案 */
  opacity: 0.3;
  z-index: 0;
}

/* 模糊效果的光晕形状 */
.home-page::after {
  content: '';
  position: absolute;
  top: 20%;
  left: 25%;
  width: 400px;
  height: 400px;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.4) 0%, rgba(255, 255, 255, 0) 70%);
  filter: blur(150px);
  z-index: 0;
}

/* 标题样式及动画 */
h1 {
  font-family: 'ZCOOL KuaiLe', cursive;
  font-size: 3.7rem;
  color: #5d4037;
  margin-bottom: 40px;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
  animation: titlePop 1s ease-out, glow 2s ease-in-out infinite alternate;
  position: relative;
  font-weight: 300;
  /* 尝试降低字体粗细 */
}

/* 旋转帽子图标 */
.hat-icon {
  position: absolute;
  top: -20px;
  right: -30px;
  font-size: 1.5rem;
  animation: spin 2s linear infinite;
  cursor: pointer;
  /* 添加鼠标指针样式，表示可以点击 */
}

/* 搜索框及按钮样式 */
.search-container {
  width: 100%;
  max-width: 700px;
  margin: 0 auto 40px;
  position: relative;
  /* 确保容器的相对定位 */
}

.search-bar {
  display: flex;
  align-items: center;
  background-color: #f7f7f7;
  border-radius: 20px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  padding: 0 12px;
  transition: all 0.3s ease;
  z-index: 1;
  /* 提高搜索框的层级 */
}

.search-bar:hover {
  transform: scale(1.02);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}

.search-bar input {
  flex-grow: 1;
  height: 44px;
  font-size: 16px;
  color: #5d4037;
  background-color: transparent;
  border: none;
  outline: none;
  padding-left: 12px;
  z-index: 2;
  /* 提高输入框的层级 */
}

.search-bar input::placeholder {
  color: #d49a67;
}

.search-button {
  background-color: #d49a67;
  color: #f7f7f7;
  border: none;
  border-radius: 20px;
  height: 36px;
  padding: 0 18px;
  font-size: 14px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s ease;
  z-index: 2;
  /* 提高按钮的层级 */
}

.search-button:hover {
  background-color: #d49a67;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(109, 76, 65, 0.3);
  animation: pulse 0.8s infinite;
}

.search-bar:focus-within {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }

  50% {
    transform: scale(1.05);
  }

  100% {
    transform: scale(1);
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }

  50% {
    transform: scale(1.05);
  }

  100% {
    transform: scale(1);
  }
}

/* 更多社团按钮及动画 */
.more-clubs {
  margin-bottom: 50px;
  display: flex;
  justify-content: center;
  z-index: 10;
  /* 提高层级，确保不被其他元素遮盖 */
}

.more-clubs .explore-button {
  font-size: 16px;
  padding: 12px 26px;
  border-radius: 25px;
  background: linear-gradient(135deg, #f3cfae, #d49a67);
  /* 浅棕色渐变 */
  border: none;
  color: #fff;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  max-width: 260px;
  /* 可根据需要调整最大宽度 */
  width: 100%;
  z-index: 10;
  /* 提高按钮自身的层级 */
}

.more-clubs .explore-button span {
  z-index: 11;
  /* 确保文本层级最高 */
}

.more-clubs .explore-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.2);
  opacity: 0;
  transition: all 0.3s ease;
  border-radius: 25px;
  z-index: 10;
  /* 提高伪元素的层级 */
}

.more-clubs .explore-button:hover::before {
  opacity: 1;
  transform: scale(1);
}

.more-clubs .explore-button:hover {
  background: linear-gradient(135deg, #fcd498, #efc137);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(240, 199, 186, 0.3);
  animation: pulse 0.8s infinite;
  z-index: 10;
}

/* 排行榜样式 */
.rankings-wrapper {
  width: 100%;
  max-width: 1300px;
  z-index: 10;
}

.ranking-category {
  height: 100%;
  transition: all 0.3s ease;
  background-color: rgba(255, 255, 255, 0.8);
  /* 默认背景色 */
  border-radius: 12px;
  overflow: hidden;
  animation: fadeIn 0.5s ease-out;
  z-index: 10;
}

.ranking-category:hover {
  transform: translateY(-8px);
  /* 上弹效果更明显 */
  box-shadow: 0 12px 20px rgba(0, 0, 0, 0.1);
  background: linear-gradient(135deg, #fcf7dc, #ffcc80);
  /* 渐变背景色 */
}



.ranking-title {
  font-family: 'ZCOOL KuaiLe', cursive;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 1.5rem;
  color: #5d4037;
  padding: 18px 22px;
}

.ranking-list {
  list-style-type: none;
  padding: 0 10px;
}

.ranking-item {
  display: flex;
  align-items: center;
  padding: 14px 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  animation: fadeIn 0.5s ease-out;
  animation-fill-mode: both;
}

.ranking-item:hover {
  background-color: rgba(141, 110, 99, 0.2);
  transform: translateX(10px);
}

.ranking-item:nth-child(1) {
  animation-delay: 0.1s;
}

.ranking-item:nth-child(2) {
  animation-delay: 0.2s;
}

.ranking-item:nth-child(3) {
  animation-delay: 0.3s;
}

.ranking-position {
  background-color: #8d6e63;
  color: white;
  padding: 4px 10px;
  border-radius: 50%;
  margin-right: 12px;
  font-weight: bold;
  min-width: 28px;
  text-align: center;
  transition: background-color 0.3s ease;
}

.ranking-item:hover .ranking-position {
  background-color: #e73c7e;
}

.ranking-name {
  flex-grow: 1;
  margin-left: 12px;
  color: #5d4037;
  font-size: 1rem;
}

.el-icon {
  color: #8d6e63;
  font-size: 18px;
  transition: all 0.3s ease;
}

.ranking-item:hover .el-icon {
  animation: rotate 1s linear infinite;
  color: #ff0015;
}

/* 社团分类特殊样式 */
.ranking-category:last-child .ranking-item {
  justify-content: center;
}

.ranking-category:last-child .ranking-position {
  display: none;
}

.ranking-category:last-child .ranking-name {
  font-weight: bold;
  font-size: 1.1rem;
}

/* 图片容器 */
.image-container {
  position: relative;
  width: 100%;
  height: 100px;
}

.bottom-image {
  position: absolute;
  bottom: -60px;
  right: 10px;
  width: 180px; /* 设置图片的宽度 */
  height: auto;
}

@media (max-width: 768px) {
  .home-page {
    padding: 40px 16px;
  }

  h1 {

    font-size: 2.2rem;
    margin-bottom: 30px;
  }

  .search-container {
    margin-bottom: 30px;
  }

  .search-bar {
    max-width: 90%;
  }

  .rankings-row {
    flex-direction: column;
  }

  .ranking-category {
    margin-bottom: 20px;
  }

  .ranking-title {
    font-size: 1.3rem;
    padding: 14px 18px;
  }

  .ranking-name {
    font-size: 0.95rem;
  }

  .ranking-category:last-child .ranking-name {
    font-size: 1rem;
  }
}

/* 新增动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.ranking-category {
  animation: fadeIn 0.5s ease-out;
}

.ranking-item {
  animation: fadeIn 0.5s ease-out;
  animation-fill-mode: both;
}

.ranking-item:nth-child(1) {
  animation-delay: 0.1s;
}

.ranking-item:nth-child(2) {
  animation-delay: 0.2s;
}

.ranking-item:nth-child(3) {
  animation-delay: 0.3s;
}
</style>
